package io.xxx.xbutler.core.callback;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import io.xxx.xbutler.data.RobotLoginMapper;
import io.xxx.xbutler.domain.Callback;
import io.xxx.xbutler.domain.RobotLogin;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * 扫码号登录需安全验证回调
 * <p>
 * 商家登录扫码号时，平台出码且商家扫码后，如果需要通过验证码验证登录，通过此回调通知商家，用户手机上会显示验证码；商家收到此回调后调用扫码号登录提交安全验证码接口上传验证码。
 *
 * <pre>
 * {
 *     "nType": 1081,
 *     "vcMerchantNo": "",
 *     "vcRobotWxId": "",
 *     "vcRobotSerialNo": "",
 *     "vcSerialNo": "",
 *     "nResult": 1,
 *     "vcResult": "SUCCESS",
 *     "Data": null,
 *     "nTimeStamp": 1623812788547
 * }
 * </pre>
 */
@Slf4j
@Component
public class _1081Handler implements CallbackHandler {

    @Resource
    private RobotLoginMapper robotLoginMapper;

    @Override
    public String name() {
        return "扫码号登录需安全验证回调";
    }

    @Override
    public boolean supports(int type) {
        return type == 1081;
    }

    @Override
    public void handle(Callback callback) {
        LambdaUpdateWrapper<RobotLogin> wrapper = new LambdaUpdateWrapper<RobotLogin>()
                .set(RobotLogin::getVerificationCode, callback.getData().toString())
                .eq(RobotLogin::getSerialNo, callback.getSerialNo());
        robotLoginMapper.update(wrapper);
    }
}
